import  xlsxwriter

workbook = xlsxwriter.Workbook('饼状图.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': 1})
# --------1、准备数据并写⼊excel--------------
# 向excel中写⼊数据，建⽴图标时要⽤到
data = [
    ['closed', 'active', 'reopen', 'NT'],
    [1012, 109, 123, 131],
 ]

# 写⼊数据
worksheet.write_row('A1', data[0], bold)
worksheet.write_row('A2', data[1])

chart_col = workbook.add_chart({'type': 'pie'})
# --------2、⽣成图表并插⼊到excel---------------
# 创建⼀个柱状图(pie chart)
chart_col = workbook.add_chart({'type':
'pie'})
 # 配置第⼀个系列数据
chart_col.add_series({
 'name': 'Bug Analysis',
 'categories': '=Sheet1!$A$1:$D$1',
 'values': '=Sheet1!$A$2:$D$2',
 'points': [
        {'fill': {'color': '#00CD00'}},
        {'fill': {'color': 'red'}},
        {'fill': {'color': 'yellow'}},
        {'fill': {'color': 'gray'}},
    ],
})

chart_col.set_title({'name': '饼状图'})
chart_col.set_x_axis({'name': 'x轴说明'})
chart_col.set_y_axis({'name': 'y轴说明'})
chart_col.set_style(2)
worksheet.insert_chart('A10', chart_col,{'x_offset': 30, 'y_offset': 30})
workbook.close()
